﻿using RpcHelper;
using RpcHelper.Validate;
using RpcModel;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Model
{
    /// <summary>
    /// 负载节点方案
    /// </summary>
    public class TransmitBasicAdd
    {

        /// <summary>
        /// 集群Id
        /// </summary>
        [NumValidate("rpc.store.mer.id.error", 1)]
        public long RpcMerId
        {
            get;
            set;
        }
        /// <summary>
        /// 方案名称(默认方案传：Def)
        /// </summary>
        [LenValidate("rpc.store.transmit.scheme.len", 2, 50)]
        [FormatValidate("rpc.store.transmit.scheme.error", ValidateFormat.纯字母)]
        public string Scheme
        {
            get;
            set;
        }
        /// <summary>
        /// 使用的服务器节点Id
        /// </summary>
        [NumValidate("rpc.store.server.id.error", 1)]
        public long SeverId
        {
            get;
            set;
        }
        /// <summary>
        /// 负载均衡类型
        /// </summary>
        [EnumValidate("rpc.store.transmit.type.error", typeof(TransmitType))]
        public TransmitType TransmitType
        {
            get;
            set;
        }
        /// <summary>
        /// 负载配置
        /// </summary>
        [NullValidate("rpc.store.transmit.config.null")]
       [EntrustValidate("_Check")]
        public TransmitConfig[] TransmitConfig
        {
            get;
            set;
        }
      
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable
        {
            get;
            set;
        }
        private bool _Check(out string error)
        {
            if(this.TransmitType == TransmitType.close)
            {
                error = null;
                return true;
            }
            else if (this.TransmitType == TransmitType.FixedType)
            {
                if (this.TransmitConfig.IsExists(a => a.Value.IsNull()))
                {
                    error = "rpc.store.transmit.fixed.value.null";
                    return false;
                }
                error = null;
                return true;
            }
            else if (this.TransmitConfig.IsExists(a => a.Range.IsNull()))
            {
                error = "rpc.store.transmit.range.null";
                return false;
            }
            error = null;
            return true;
        }
    }
}
